package operate

import (
	"fmt"

	"gitee.com/micro-plat/uos/uos/const/conf"
	"gitee.com/micro-plat/uos/uos/const/sql"
	"github.com/micro-plat/hydra/components"
)

//DBCreate 新增用户操作日志数据
func dbCreate(info *OperateInfo) error {
	arg := constructExtention(info)
	dbe := components.Def.DB().GetRegularDB(conf.DBName)
	if _, err := dbe.Execute(sql.SaveOperateInfo, arg); err != nil {
		return fmt.Errorf("保存用户操作数据发生错误,err:%v", err)
	}
	return nil
}

func constructExtention(info *OperateInfo) map[string]interface{} {
	arg := map[string]interface{}{
		"user_name":   info.UserName,
		"user_id":     info.UserID,
		"log_module":  info.LogModule,
		"log_content": info.LogContent,
		"req_url":     info.RequestURL,
		"req_body":    info.RequestBody,
		"user_ip":     info.UserIP,
		"object_no1":  "",
		"object_no2":  "",
	}
	new := info.Etentions
	if val, flag := new["object_no1"]; flag {
		arg["object_no1"] = val
	}
	if val, flag := new["object_no2"]; flag {
		arg["object_no2"] = val
	}
	return arg
}
